import webview, platform
from threading import Thread

win = None

class Api:
  def call_python(self, msg):
    print(f"js call python: {msg}")
    return 'pong'

def preload_js():
  win.evaluate_js(f"preload('python v{platform.python_version()}')")

def call_js():
  win.evaluate_js("call_js('hello')")

def start_pywebview():
  global win
  win=webview.create_window(
    title='PyWebview',
    url='web/index.html',
    width=1600,
    height=800,
    resizable=True,
    fullscreen=False,
    frameless=False,
    easy_drag=False,
    js_api=Api()
  )
  Thread(target=call_js).start() # New thread, run anywhere
  win.state.foo = 'bar'
  win.events.loaded += preload_js # Non-blocking, only execute on load
  webview.start(debug=True) # Blocking

def main():
  start_pywebview() # Must be placed last

if __name__ == "__main__":
  main()
